using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public abstract class OnlineDataController
{
    public abstract string getAllStocksDataURL { get; }
    public abstract string getAllTransitionsDataURL(string stock);

    public abstract string getZhuLiZiJinURL(string stock);
    public abstract string getZiJinLiuRuURL(string stock);
    public abstract string getJieDuanZhuLiURL(string stock);
    public abstract string getChengJiaoFenBuURL(string stock);
    public abstract string getCompanyFundInfoURL(string stock);

    public abstract  Task<List<StockData>> RequestAllStocksData();
    public abstract Task<List<TransitionData>> RequestAllTransitionsData(string stockCode);

    public abstract Task<List<ZhuLiZiJinData>> RequestZhuLiZiJinData(string stockCode);

    public abstract Task<List<ZiJinLiuRuData>> RequestZiJinLiuRuData(string stockCode);

    public abstract Task<List<JieDuanZhuLiData>> RequestJieDuanZhuLiData(string stockCode);

    public abstract Task<List<ChengJiaoFenBuData>> RequestChengJiaoFenBuData(string stockCode);
    public abstract Task<CompanyFundInfo> RequestCompanyFundInfo(string stockCode);
    public async Task<List<T>> RequestListData<T>(string url)
    {
        string response = await WebUtility.HttpGet(url, null, (err) =>
        {
            GameDebug.LogError($"请求失败：{err}");
        });

        if (!string.IsNullOrEmpty(response))
        {
            try
            {
                var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(response);
                return list;
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                return null;
            }
        }
        else
        {
            GameDebug.LogError($"无返回数据");
            return null;
        }
    }
    public async Task<T> RequestData<T>(string url) where T: class
    {
        string response = await WebUtility.HttpGet(url, null, (err) =>
        {
            GameDebug.LogError($"请求失败：{err}");
        });

        if (!string.IsNullOrEmpty(response))
        {
            GameDebug.Log($"返回数据：{response}");
            return  Newtonsoft.Json.JsonConvert.DeserializeObject<T>(response);
        }
        else
        {
            GameDebug.LogError($"无返回数据");
            return null;
        }
    }
}